package team.hyznrj.studentsys.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.mongodb.BasicDBObject;
import com.mongodb.WriteResult;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import team.hyznrj.studentsys.dto.StudentMessageDto;
import team.hyznrj.studentsys.dto.StudentMessageSearchDto;
import team.hyznrj.studentsys.entity.DataReport;
import team.hyznrj.studentsys.entity.StringListEntity;
import team.hyznrj.studentsys.enums.DataReportEnum;
import team.hyznrj.studentsys.exception.AuthenticationOnFlagException;
import team.hyznrj.studentsys.exception.DataBaseObjectNull;
import team.hyznrj.studentsys.exception.DataBaseOperationError;
import team.hyznrj.studentsys.exception.DataNullException;
import team.hyznrj.studentsys.service.ExcelService;
import team.hyznrj.studentsys.service.FileService;
import team.hyznrj.studentsys.service.StudentMessageService;
import team.hyznrj.studentsys.utils.ReadExcel;
import team.hyznrj.studentsys.utils.ValueUtil;

@RestController
@RequestMapping("StudentMessage")
@Api(value = "StudentMessage")
public class StudentMessageController {
	@Autowired
	StudentMessageService service;
	@Autowired
	FileService fileService;
	@Autowired
	ExcelService excelService;

	/**
	 * 界面接口:导入操行分表(多文件导入)
	 * 
	 * @throws Exception
	 * @throws AuthenticationOnFlagException
	 * @throws DataBaseObjectNull
	 * @throws DataNullException
	 */
	@ResponseBody
	@PostMapping(value = "/importMsg")
	public Object importExcel(MultipartFile[] files, HttpServletRequest request)
			throws DataNullException, DataBaseObjectNull, AuthenticationOnFlagException, Exception {
		String flag = (String) request.getAttribute("flag");
		List<String> fileUpload = fileService.fileUpload(files, ReadExcel.EXCEL_PREDICATE);
		Object saveExcelInStudentMessage = excelService.saveExcelInStudentMessage(fileUpload, flag);
		return new DataReport(DataReportEnum.SUCCESS, null, saveExcelInStudentMessage, true);
	}

	/**
	 * 增加一个学生信息(单)
	 * 
	 * @param message
	 * @param flag
	 * @return
	 * @throws DataNullException
	 * @throws DataBaseOperationError
	 * @throws AuthenticationOnFlagException
	 * @throws DataBaseObjectNull
	 */
	@ResponseBody
	@RequestMapping(value = "/addMsg", method = { RequestMethod.POST }, produces = "application/json;charset=utf-8")
	public Object addMsg(StudentMessageDto f, HttpServletRequest request)
			throws DataNullException, DataBaseOperationError, AuthenticationOnFlagException, DataBaseObjectNull {
		String flag = (String) request.getAttribute("flag");
		System.out.println("test#" + flag);
		f.setFlag(flag);
		return new DataReport(DataReportEnum.SUCCESS, "添加成功", service.addMsg(f.getMessage(), f.getFlag()), true);
	}

	/**
	 * 增加多个学生信息
	 * 
	 * @param message
	 * @param flag
	 * @return
	 * @throws DataNullException
	 * @throws DataBaseOperationError
	 * @throws AuthenticationOnFlagException
	 * @throws DataBaseObjectNull
	 */
	@ResponseBody
	@RequestMapping(value = "/addMsgMulity", method = {
			RequestMethod.POST }, produces = "application/json;charset=utf-8")
	public Object addMsgMulity(StudentMessageDto f, HttpServletRequest request)
			throws DataNullException, DataBaseOperationError, AuthenticationOnFlagException, DataBaseObjectNull {
		String flag = (String) request.getAttribute("flag");
		f.setFlag(flag);
		return new DataReport(DataReportEnum.SUCCESS, "添加成功", service.addMsgMulity(f.getMessageList(), f.getFlag()),
				true);
	}

	/**
	 * 删除单个学生信息(_id)或通过条件删除多个学生信息
	 * 
	 * @param f
	 * @return
	 * @throws DataNullException
	 * @throws DataBaseOperationError
	 * @throws AuthenticationOnFlagException
	 * @throws DataBaseObjectNull
	 */
	@ResponseBody
	@RequestMapping(value = "/deleteMsg", method = { RequestMethod.POST }, produces = "application/json;charset=utf-8")
	public Object deleteMsg(StudentMessageDto f, HttpServletRequest request)
			throws DataNullException, DataBaseOperationError, AuthenticationOnFlagException, DataBaseObjectNull {
		String flag = (String) request.getAttribute("flag");
		f.setFlag(flag);
		return new DataReport(DataReportEnum.SUCCESS, ValueUtil.getString("成功删除?个数据", '?', 1),
				service.deleteMsg(f.getMessage(), f.getFlag()), true);
	}

	/**
	 * 通过id数组删除多个学生信息
	 * 
	 * @param f
	 * @return
	 * @throws DataNullException
	 * @throws DataBaseOperationError
	 * @throws AuthenticationOnFlagException
	 * @throws DataBaseObjectNull
	 */
	@ApiImplicitParams({ @ApiImplicitParam(name = "flag", value = "flag", paramType = "query"),
			@ApiImplicitParam(name = "idList[0]", value = "0", paramType = "query"),
			@ApiImplicitParam(name = "idList[1]", value = "1", paramType = "query"), })
	@ResponseBody
	@RequestMapping(value="/deleteMsgByKeyList",method={RequestMethod.POST} ,produces ="application/json;charset=utf-8")
	public Object deleteMsgByIdList(StudentMessageDto f,HttpServletRequest request) throws DataNullException, DataBaseOperationError, AuthenticationOnFlagException, DataBaseObjectNull{
		 String flag=(String) request.getAttribute("flag");
		 f.setFlag(flag); 
		WriteResult deleteMsg = service.deleteMsg(f.getIdList(), f.getFlag());
		return new DataReport(DataReportEnum.SUCCESS,ValueUtil.getString("成功删除?行数据", '?', deleteMsg.getN()), deleteMsg,true);
	}
	/**
	 * 更新信息 单更新:传入_id则认为是单更新 否则，则按匹配的条件进行查询
	 * 
	 * @param f
	 * @return
	 * @throws DataNullException
	 * @throws DataBaseOperationError
	 * @throws AuthenticationOnFlagException
	 * @throws DataBaseObjectNull
	 */
	@ResponseBody
	@RequestMapping(value = "/updateMsg", method = { RequestMethod.POST }, produces = "application/json;charset=utf-8")
	public Object updateMsg(StudentMessageDto f, HttpServletRequest request)
			throws DataNullException, DataBaseOperationError, AuthenticationOnFlagException, DataBaseObjectNull {
		String flag = (String) request.getAttribute("flag");
		f.setFlag(flag);
		WriteResult updateMsg = service.updateMsg(f.getConditions(), f.getNewMessage(), f.getFlag());
		return new DataReport(DataReportEnum.SUCCESS, ValueUtil.getString("成功更新?个数据", '?', updateMsg.getN()), updateMsg,
				true);
	}

	/**
	 * 按学号组进行更新信息(注意不是_id,是学号对应的id)
	 * 
	 * @param f
	 * @return
	 * @throws DataNullException
	 * @throws DataBaseOperationError
	 * @throws AuthenticationOnFlagException
	 * @throws DataBaseObjectNull
	 */
	@ResponseBody
	@RequestMapping(value = "/updateMsgByKeyList", method = {
			RequestMethod.POST }, produces = "application/json;charset=utf-8")
	public Object updateMsgByKeyList(StudentMessageDto f, HttpServletRequest request)
			throws DataNullException, DataBaseOperationError, AuthenticationOnFlagException, DataBaseObjectNull {
		String flag = (String) request.getAttribute("flag");
		f.setFlag(flag);
		String updateMsgByKeyList = service.updateMsgByKeyList(f.getIdList(), f.getNewMessage(), f.getFlag());
		return new DataReport(DataReportEnum.SUCCESS, updateMsgByKeyList, updateMsgByKeyList, true);
	}

	/**
	 * 获取信息
	 * 
	 * @param f
	 * @return
	 * @throws DataNullException
	 * @throws DataBaseOperationError
	 * @throws AuthenticationOnFlagException
	 * @throws DataBaseObjectNull
	 */
	@ResponseBody
	@RequestMapping(value = "/getMsg", method = { RequestMethod.POST }, produces = "application/json;charset=utf-8")
	public Object getMsg(StudentMessageDto f, HttpServletRequest request)
			throws DataNullException, DataBaseOperationError, AuthenticationOnFlagException, DataBaseObjectNull {
		String flag = (String) request.getAttribute("flag");
		f.setFlag(flag);
		List<BasicDBObject> msg = service.getMsg(f.getConditions(), f.getFlag(), f.isDirection());
		return new DataReport(DataReportEnum.SUCCESS, null, msg, true);
	}

	/**
	 * 获取查询条件获取学号数组
	 * 
	 * @param
	 * @return
	 */
	@ResponseBody
	@PostMapping(value = "/getList")
	public Object getPIDByKeywords(StudentMessageSearchDto dto, HttpServletRequest request) {
		String flag = (String) request.getAttribute("flag");
		dto.setFlag(flag);
		List<String> list = service.getPIDByKeywords(dto);
		return new DataReport(list != null ? DataReportEnum.SUCCESS : DataReportEnum.FIND_ERROR, null, list,
				list != null);
	}

	/**
	 * 获取学院字段聚合的信息
	 * 
	 * @param
	 * @return
	 */
	@ResponseBody
	@PostMapping(value = "/getSearchKeys")
	public Object getAggregateKeysList(String keyWords, @Param("flag") String flag) {
		List<String> list = service.getAggregateKeyWords(keyWords, flag);
		return new DataReport(list != null ? DataReportEnum.SUCCESS : DataReportEnum.FIND_ERROR, null, list,
				list != null);
	}
}
